from datetime import datetime
from typing import List, Union, Literal
from sqlmodel import SQLModel, Field


class SystemMenu(SQLModel, table=True):
    __tablename__ = "system_menu"

    id: int = Field(default=None, primary_key=True, index=True, nullable=False)
    pid: int = Field(default=0, nullable=False)
    type: str = Field(default='', max_length=2, nullable=False)
    name: str = Field(default='', max_length=100, nullable=False)
    icon: str = Field(default='', max_length=100, nullable=False)
    sort: int = Field(default=0, nullable=False)
    perms: str = Field(default='', max_length=100, nullable=False)
    paths: str = Field(default='', max_length=100, nullable=False)
    component: str = Field(default='', max_length=200, nullable=False)
    selected: str = Field(default='', max_length=200, nullable=False)
    params: str = Field(default='', max_length=200, nullable=False)
    is_cache: int = Field(default=0, nullable=False)
    is_show: int = Field(default=1, nullable=False)
    is_disable: int = Field(default=0, nullable=False)
    create_time: datetime = Field(default_factory=datetime.utcnow, nullable=False)
    update_time: datetime = Field(default_factory=datetime.utcnow, sa_column_kwargs={'onupdate': datetime.utcnow}, nullable=False)


class SystemMenuCreateIn(SQLModel):
    """新增菜单参数"""
    pid: int = Field(ge=0)  # 上级菜单
    type: Literal['M', 'C', 'A'] = Field(alias='menuType')  # 权限类型: [M=目录, C=菜单, A=按钮]
    name: str = Field(alias='menuName', min_length=1, max_length=30)  # 菜单名称
    icon: Union[str, None] = Field(alias='menuIcon', max_length=100)  # 菜单图标
    sort: int = Field(alias='menuSort', ge=0)  # 菜单排序
    perms: Union[str, None] = Field(max_length=100)  # 权限标识
    paths: Union[str, None] = Field(max_length=200)  # 路由地址
    component: Union[str, None] = Field(max_length=200)  # 前端组件
    selected: Union[str, None] = Field(max_length=200)  # 选中路径
    params: Union[str, None] = Field(max_length=200)  # 路由参数
    is_cache: int = Field(alias='isCache', ge=0, le=1)  # 是否缓存: [0=否, 1=是]
    is_show: int = Field(alias='isShow', ge=0, le=1)  # 是否显示: [0=否, 1=是]
    is_disable: int = Field(alias='isDisable', ge=0, le=1)  # 是否禁用: [0=否, 1=是]


class SystemMenuEditIn(SystemMenuCreateIn):
    """编辑菜单参数"""
    id: int = Field(gt=0)  # 主键